<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺id" prop="storeId">
        <el-select v-model="queryParams.storeId" placeholder="店铺账号" clearable>
          <el-option
            v-for="item in storeList"
            :key="item.value"
            :label="item.lable"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动名称" prop="promotionName">
        <el-input
          v-model="queryParams.promotionName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="活动id" prop="promotionId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.promotionId"-->
      <!--          placeholder="请输入活动id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="结束时间" prop="endTime">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.endTime"-->
      <!--          placeholder="请输入结束时间"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="开始时间" prop="startTime">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.startTime"-->
      <!--          placeholder="请输入开始时间"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['system:sellerStorePromotionActivity:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['system:sellerStorePromotionActivity:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sellerStorePromotionActivity:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sellerStorePromotionActivity:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sellerStorePromotionActivityList" stripe border
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="店铺名称" align="center" prop="storeName"/>
      <el-table-column label="活动名称" align="center" prop="promotionName"/>
      <!--      <el-table-column label="活动折扣" align="center" prop="existStoreFansDiscount"/>-->
      <!--      <el-table-column label="活动id" align="center" prop="promotionId"/>-->
      <!--      <el-table-column label="是否大促" align="center" prop="hasPromo">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="hasPromoList" :value="scope.row.hasPromo"></dict-tag>-->
      <!--          <div v-if="scope.row.hasPromo == 'true'">是</div>-->
      <!--          <div v-else>否</div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="结束时间" align="center" prop="endTime"/>
      <el-table-column label="开始时间" align="center" prop="startTime"/>
      <!--      <el-table-column label="状态" align="center" prop="status">-->
      <!--        <template slot-scope="scope">-->
      <!--          <div v-if="scope.row.erpStatus == 'NotStarted' ">未开始</div>-->
      <!--          <div v-if="scope.row.erpStatus == 'Ongoing' ">进行中</div>-->
      <!--          <div v-if="scope.row.erpStatus == 'Invalid' ">已暂停</div>-->
      <!--          <div v-if="scope.row.erpStatus == 'Finished' ">已结束</div>-->
      <!--          <div v-else>未发布</div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="erp内部状态" align="center" prop="erpStatus">-->
      <!--        <template slot-scope="scope">-->
      <!--          <div v-if="scope.row.erpStatus == 0 ">待发布</div>-->
      <!--          <div v-else-if="scope.row.erpStatus == 1 ">已发布</div>-->
      <!--          <div v-else>已删除</div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button size="mini" type="text" @click="releaseToSmt(scope.row)">发布至速卖通</el-button>-->
          <!--          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">修改</el-button>-->
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商家促销活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名称" prop="storeId">
          <el-select v-model="form.storeId" placeholder="请选择店铺" clearable>
            <el-option
              v-for="item in storeList"
              :key="item.value"
              :label="item.lable"
              :value="item.value"
            />
          </el-select>
          <!--          <el-input v-model="form.storeId" placeholder="请输入店铺id"/>-->
        </el-form-item>
        <el-form-item label="活动名称" prop="promotionName">
          <el-input v-model="form.promotionName" placeholder="请输入活动名称"/>
        </el-form-item>
        <!--        <el-form-item label="活动id" prop="promotionId" v-if="form.promotionId">-->
        <!--          <el-input disabled v-model="form.promotionId" placeholder="请输入活动id"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="是否大促" prop="hasPromo">-->
        <!--          <el-select v-model="form.hasPromo">-->
        <!--            <el-option v-for="(item,index) in hasPromoList" :key="index" :label="item.label"-->
        <!--                       :value="item.value"></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="活动折扣" prop="existStoreFansDiscount">
          <el-input v-model="form.existStoreFansDiscount" placeholder="请输入活动折扣"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker v-model="form.startTime" type="datetime" value-format="yyyy-MM-dd hh:mm:ss"
                          placeholder="选择活动开始时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker v-model="form.endTime" type="datetime" value-format="yyyy-MM-dd hh:mm:ss"
                          placeholder="选择活动结束时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSellerStorePromotionActivity,
  getSellerStorePromotionActivity,
  delSellerStorePromotionActivity,
  addSellerStorePromotionActivity,
  updateSellerStorePromotionActivity, createActivityToAe
} from "@/api/marketActivity/marketActivity";
import {queryUserShopList} from "@/api/shop";

export default {
  name: "SellerStorePromotionActivity",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家促销活动表格数据
      sellerStorePromotionActivityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId: undefined,
        promotionName: undefined,
        promotionId: undefined,
        hasPromo: undefined,
        endTime: undefined,
        startTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        activityId: [
          {required: false, message: "主键不能为空", trigger: "blur"}
        ],
        userId: [
          {required: false, message: "用户id不能为空", trigger: "blur"}
        ],
        storeId: [
          {required: true, message: "店铺id不能为空", trigger: "change"}
        ],
        promotionName: [
          {required: true, message: "活动名称不能为空", trigger: "blur"}
        ],
        promotionId: [
          {required: true, message: "活动id不能为空", trigger: "blur"}
        ],
        hasPromo: [
          {required: false, message: "是否大促不能为空", trigger: "change"}
        ],
        endTime: [
          {required: true, message: "结束时间不能为空", trigger: "change"}
        ],
        startTime: [
          {required: true, message: "开始时间不能为空", trigger: "change"}
        ],
        existStoreFansDiscount: [{required: true, message: "开始时间不能为空", trigger: "blur"}],
      },
      hasPromoList: [{
        label: "是",
        value: true
      }, {
        label: "否",
        value: false
      }],
      userInfo: "",
      storeList: [],//店铺列表
    };
  },
  created() {
    this.userInfo = this.$store.state.user.urserInfo
    console.log('userInfo', this.userInfo)
    this.getEmpowerList()
  },
  methods: {
    /** 查询商家促销活动列表 */
    getList() {
      this.loading = true;
      if (!this.queryParams.storeId) {
        this.queryParams.storeId = this.storeList[0].value
      }
      listSellerStorePromotionActivity(this.queryParams).then(response => {
        this.sellerStorePromotionActivityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取店铺账号选择列表
    getEmpowerList() {
      queryUserShopList().then(res => {
        this.storeList = res.data
        this.getList();
      });
    },
    // 发布到速卖通
    releaseToSmt(row) {
      this.$confirm('是否确定发布该营销活动到速卖通？', '提示').then(() => {
        createActivityToAe({activityId: row.activityId}).then(res => {
          this.$message.success(res.msg)
        })
      })

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        activityId: undefined,
        userId: this.userInfo.userId,
        storeId: undefined,
        promotionName: undefined,
        promotionId: undefined,
        hasPromo: false,
        existStoreFansDiscount: undefined,
        endTime: undefined,
        startTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.activityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家促销活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const activityId = row.activityId || this.ids
      getSellerStorePromotionActivity(activityId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改商家促销活动";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.activityId != null) {
            updateSellerStorePromotionActivity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSellerStorePromotionActivity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const activityIds = row.activityId || this.ids;
      this.$modal.confirm('是否确认删除商家促销活动编号为"' + activityIds + '"的数据项？').then(() => {
        this.loading = true;
        return delSellerStorePromotionActivity(activityIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sellerStorePromotionActivity/export', {
        ...this.queryParams
      }, `sellerStorePromotionActivity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
